﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using WMSADBUtility;
using WMSAML.Warehouse;

namespace WMSADL.Warehouse
{
    public class PD_PostionToLayoutDAL
    {
        #region  Method
        public bool Exists(int NID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PD_PostionToLayout where NID=@NID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public int Add(PD_PostionToLayoutEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PD_PostionToLayout(");
            strSql.Append("Postion_NID,PositionNO,Layout_NID,Layout_Field,PointTop,PointL,PointR,PointU,PointD,CreateTime,Stauts,Mark,field1,field2,field3,field4,field5)");

            strSql.Append(" values (");
            strSql.Append("@Postion_NID,@PositionNO,@Layout_NID,@Layout_Field,@PointTop,@PointL,@PointR,@PointU,@PointD,@CreateTime,@Stauts,@Mark,@field1,@field2,@field3,@field4,@field5)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Postion_NID", DbType.Int32, entity.Postion_NID);
            db.AddInParameter(dbCommand, "PositionNO", DbType.String, entity.PositionNO);
            db.AddInParameter(dbCommand, "Layout_NID", DbType.Int32, entity.Layout_NID);
            db.AddInParameter(dbCommand, "Layout_Field", DbType.String, entity.Layout_Field);
            db.AddInParameter(dbCommand, "PointTop", DbType.String, entity.PointTop);
            db.AddInParameter(dbCommand, "PointL", DbType.String, entity.PointL);
            db.AddInParameter(dbCommand, "PointR", DbType.String, entity.PointR);
            db.AddInParameter(dbCommand, "PointU", DbType.String, entity.PointU);
            db.AddInParameter(dbCommand, "PointD", DbType.String, entity.PointD);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "Stauts", DbType.Int32, entity.Stauts);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PD_PostionToLayoutEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PD_PostionToLayout set ");
            strSql.Append("Postion_NID=@Postion_NID,");
            strSql.Append("PositionNO=@PositionNO,");
            strSql.Append("Layout_NID=@Layout_NID,");
            strSql.Append("Layout_Field=@Layout_Field,");
            strSql.Append("PointTop=@PointTop,");
            strSql.Append("PointL=@PointL,");
            strSql.Append("PointR=@PointR,");
            strSql.Append("PointU=@PointU,");
            strSql.Append("PointD=@PointD,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Stauts=@Stauts,");
            strSql.Append("Mark=@Mark,");
            strSql.Append("field1=@field1,");
            strSql.Append("field2=@field2,");
            strSql.Append("field3=@field3,");
            strSql.Append("field4=@field4,");
            strSql.Append("field5=@field5");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, entity.NID);
            db.AddInParameter(dbCommand, "Postion_NID", DbType.Int32, entity.Postion_NID);
            db.AddInParameter(dbCommand, "PositionNO", DbType.String, entity.PositionNO);
            db.AddInParameter(dbCommand, "Layout_NID", DbType.Int32, entity.Layout_NID);
            db.AddInParameter(dbCommand, "Layout_Field", DbType.String, entity.Layout_Field);
            db.AddInParameter(dbCommand, "PointTop", DbType.String, entity.PointTop);
            db.AddInParameter(dbCommand, "PointL", DbType.String, entity.PointL);
            db.AddInParameter(dbCommand, "PointR", DbType.String, entity.PointR);
            db.AddInParameter(dbCommand, "PointU", DbType.String, entity.PointU);
            db.AddInParameter(dbCommand, "PointD", DbType.String, entity.PointD);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "Stauts", DbType.Int32, entity.Stauts);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PostionToLayoutEntity GetEntity(int NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,Postion_NID,PositionNO,Layout_NID,Layout_Field,PointTop,PointL,PointR,PointU,PointD,CreateTime,Stauts,Mark,field1,field2,field3,field4,field5 from PD_PostionToLayout ");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            PD_PostionToLayoutEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }

        /// <summary>
        /// GetEntityByPotionNID
        /// </summary>
        public PD_PostionToLayoutEntity GetEntityByPotionNID(int Postion_NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,Postion_NID,PositionNO,Layout_NID,Layout_Field,PointTop,PointL,PointR,PointU,PointD,CreateTime,Stauts,Mark,field1,field2,field3,field4,field5 from PD_PostionToLayout ");
            strSql.Append(" where Postion_NID=@Postion_NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Postion_NID", DbType.Int32, Postion_NID);
            PD_PostionToLayoutEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PostionToLayoutEntity DataRowToEntity(DataRow row)
        {
            PD_PostionToLayoutEntity entity = new PD_PostionToLayoutEntity();
            if (row != null)
            {
                if (row["NID"] != null && row["NID"].ToString() != "")
                {
                    entity.NID = int.Parse(row["NID"].ToString());
                }
                if (row["Postion_NID"] != null && row["Postion_NID"].ToString() != "")
                {
                    entity.Postion_NID = int.Parse(row["Postion_NID"].ToString());
                }
                if (row["PositionNO"] != null)
                {
                    entity.PositionNO = row["PositionNO"].ToString();
                }
                if (row["Layout_NID"] != null && row["Layout_NID"].ToString() != "")
                {
                    entity.Layout_NID = int.Parse(row["Layout_NID"].ToString());
                }
                if (row["Layout_Field"] != null)
                {
                    entity.Layout_Field = row["Layout_Field"].ToString();
                }
                if (row["PointTop"] != null)
                {
                    entity.PointTop = row["PointTop"].ToString();
                }
                if (row["PointL"] != null)
                {
                    entity.PointL = row["PointL"].ToString();
                }
                if (row["PointR"] != null)
                {
                    entity.PointR = row["PointR"].ToString();
                }
                if (row["PointU"] != null)
                {
                    entity.PointU = row["PointU"].ToString();
                }
                if (row["PointD"] != null)
                {
                    entity.PointD = row["PointD"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    entity.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Stauts"] != null && row["Stauts"].ToString() != "")
                {
                    entity.Stauts = int.Parse(row["Stauts"].ToString());
                }
                if (row["Mark"] != null)
                {
                    entity.Mark = row["Mark"].ToString();
                }
                if (row["field1"] != null)
                {
                    entity.field1 = row["field1"].ToString();
                }
                if (row["field2"] != null)
                {
                    entity.field2 = row["field2"].ToString();
                }
                if (row["field3"] != null)
                {
                    entity.field3 = row["field3"].ToString();
                }
                if (row["field4"] != null)
                {
                    entity.field4 = row["field4"].ToString();
                }
                if (row["field5"] != null)
                {
                    entity.field5 = row["field5"].ToString();
                }
            }
            return entity;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,Postion_NID,PositionNO,Layout_NID,Layout_Field,PointTop,PointL,PointR,PointU,PointD,CreateTime,Stauts,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_PostionToLayout ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<PD_PostionToLayoutEntity> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,Postion_NID,PositionNO,Layout_NID,Layout_Field,PointTop,PointL,PointR,PointU,PointD,CreateTime,Stauts,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_PostionToLayout ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<PD_PostionToLayoutEntity> list = new List<PD_PostionToLayoutEntity>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public PD_PostionToLayoutEntity ReaderBind(IDataReader dataReader)
        {
            PD_PostionToLayoutEntity entity = new PD_PostionToLayoutEntity();
            object ojb;
            ojb = dataReader["NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.NID = (int)ojb;
            }
            ojb = dataReader["Postion_NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.Postion_NID = (int)ojb;
            }
            entity.PositionNO = dataReader["PositionNO"].ToString();
            ojb = dataReader["Layout_NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.Layout_NID = (int)ojb;
            }
            entity.Layout_Field = dataReader["Layout_Field"].ToString();
            entity.PointTop = dataReader["PointTop"].ToString();
            entity.PointL = dataReader["PointL"].ToString();
            entity.PointR = dataReader["PointR"].ToString();
            entity.PointU = dataReader["PointU"].ToString();
            entity.PointD = dataReader["PointD"].ToString();
            ojb = dataReader["CreateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CreateTime = (DateTime)ojb;
            }
            ojb = dataReader["Stauts"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.Stauts = (int)ojb;
            }
            entity.Mark = dataReader["Mark"].ToString();
            entity.field1 = dataReader["field1"].ToString();
            entity.field2 = dataReader["field2"].ToString();
            entity.field3 = dataReader["field3"].ToString();
            entity.field4 = dataReader["field4"].ToString();
            entity.field5 = dataReader["field5"].ToString();
            return entity;
        }

        #endregion  Method
    }
}
